#coding=utf-8
import threading,time,common,json,sys,random;
from Lib.baidu.common_core import CommonCore;
from Lib.baidu_mobile.common_core import MCommonCore;
from Lib.baidu_mobile.related_core import MRelatedCore;
from Lib.so360.common_core import So360CommonCore;
from Lib.so360.related_core import So360RelatedCore;
from Lib.sogou.common_core import SogouCommonCore;
from Lib.sogou.related_core import SogouRelatedCore;
from Lib.baidu.related_core import RelatedCore;
from Lib.baidu.index_core import BaiduIndexCore;
import configparser
#读取配置文件
config=configparser.ConfigParser()
config.read('basic.conf');
data = {'vps_id':config['basic']['vps_id']};
vps_id = config['basic']['vps_id'];
#获取远程服务器的数据
netData = common.getUrlRequest(config['url']['get_data_url'],data)
netData = json.loads(netData)

list = []
if(netData['error']==0):
    list = netData['data'];
if(len(list)==0):
    print('并没有数据');
    sys.exit()
times = 50;#循环次数,请大于项目里最大的运行次数，因为项目失败的时候次数不会-1
#唯一识别code,关键词，信息流，翻页数，运行次数,id,类型
#list = [
    #['zyjy','房产',['南方房产网'],10,40,1,'1']
   
    #]
appTimes = 0;#计算运行次数
lastIP = '';
#type 类型1普通2下拉3相关
def run(keyword,clickWord,pages,code,id,type,vps_id):
    if(type == '2'):
        #baidu下拉
        print('===【百度搜索-下拉】===');
        m  = RelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '3'):
        print('===【百度搜索-相关】===');
        m  = RelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '1'):
        print('===【百度搜索-点击】===');
        m  = CommonCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '4'):
        print('===【百度手机搜索-点击】===');
        m  = MCommonCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '5'):
        #baidu移动下拉
        print('===【百度手机搜索-下拉】===');
        m  = MRelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '6'):
        #百度移动相关
        print('===【百度手机搜索-相关】===');
        m  = MRelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '7'):
        print('===【360搜索-点击】===');
        m  = So360CommonCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '8'):
        #360下拉
        print('===【360搜索-下拉】===');
        m  = So360RelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '9'):
        #相关
        print('===【360搜索-相关】===');
        m  = So360RelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '13'):
        #sougou点击
        print('===【搜狗搜索-点击】===');
        m  = SogouCommonCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '14'):
        #sougou下拉
        print('===【搜狗搜索-下拉】===');
        m  = SogouRelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '15'):
        print('===【搜狗搜索-相关】===');
        m  = SogouRelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    elif(type == '16'):
        print('===【百度搜索-指数】===');
        m  = BaiduIndexCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)
    else:
        m  = So360RelatedCore(keyword=keyword,clickWord = clickWord,pages = pages,success = 1,code=code,id=id,type=type,vps_id = vps_id)

    
    m.setCfg(find_all=0)
    return m.loop()
def main():
    global lastIP;
    global appTimes;
    result_log= {};
   
    
    while(times>appTimes):
       
        try:
            ip = common.get_out_ip();
            #random.randint(1,15);
        except:
            #获取ip失败则返回上一个ip
            print('【!】ip获取异常');
            ip = lastIP;
        if(ip!=lastIP):
            lastIP = ip
        else:
            print(ip+'ip没有变化，10s后重试');
            time.sleep(10)
            main()  
        appTimes = appTimes+1
        print('>>>第'+str(appTimes)+'次循环');
        isRun = 0;#判断是否所有词都运行完毕
        for i in range(len(list)):
            param = {
                'keyword':list[i][1],
                'click_word':list[i][2],
                'pages':int(list[i][3]),
                'code':list[i][0],
                'id':list[i][5],
                'type':list[i][6]
                }
            #print(param);
            list[i][4] = int(list[i][4]);#点击次数
            if(appTimes==1 and list[i][4]==0):
                #如果第一次循环的时候,运行次数=0就随机运行次数
                list[i][4] = random.randint(5, 15);
            if(list[i][4] >0):
                isRun = 1;
                if result_log.get(i)== None:result_log[i] = 0;
                result = run(list[i][1],list[i][2],int(list[i][3]),list[i][0],list[i][5],list[i][6],vps_id)
                result_log[i] = result_log[i]+result;
                if(result==1):
                    list[i][4]  = list[i][4] -1
                print('>>剩余次数：'+str(list[i][4])+'次');
                print('>>成功次数：'+str(result_log[i])+'次');
                print('################################');
                time.sleep(15);
        if(isRun==0):
           print('运行完毕');
           sys.exit();
if __name__ == '__main__':
    main()


   
